package com.kx.converter;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateConverter {

    public static String DATE_YYYY_MM_DD = "yyyy-MM-dd";

    public static String DATE_Y_M_DDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 数据库默认日期（Sat Mar 26 20:32:14 CST 2022）转长整型
     * @param sql
     * @return
     */
    public static long myConvert2long(String sql){
        long l = 0;
        try {
            Date date=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK).parse(sql);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
            String sDate=sdf.format(date);
            l = convert2long(sDate, "yyyy-MM-dd HH:ss:mm");
//            System.out.println(l);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return l;
    }

    /**
     * 将日期格式的字符串转为长整型
     * @param date
     * @param format
     * @return
     */
    public static long convert2long(String date, String format) {
        try {
            if (StringUtils.isNotBlank(date) && StringUtils.isNotBlank(format)) {
                SimpleDateFormat sf = new SimpleDateFormat(format);
                return sf.parse(date).getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }

    /**
     * 长整型数字转日期格式的字符串
     * @param time
     * @param format
     * @return
     */
    public static String convert2String(long time, String format) {
        if (time > 0l && StringUtils.isNotBlank(format)) {
            SimpleDateFormat sf = new SimpleDateFormat(format);
            Date date = new Date(time);
            return sf.format(date);
        }
        return "";
    }

    /**
     * 获取当前系统日期
     * @return
     */
    public static long curTimeMillis() {
        return System.currentTimeMillis();
    }
}
